package com.app.business.service;

import com.app.business.model.FileInfo;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 档案资料业务层
 *
 * @author lsh
 * @date 2019-05-09
 */
@SuppressWarnings("ALL")
@Service
public class FileInfoService extends JpaBaseDao<FileInfo, Integer> {

    /**
     * 档案查询
     *
     * @param contractId   家庭id
     * @param fileCategory 档案目录代码
     * @param status       删除状态
     * @return
     */
    public List<FileInfo> getFileInfoList(Integer contractId, String fileCategory, Integer status) {
        String hql = "from FileInfo f inner join fetch f.contract c where c.id =:contractId and f.fileCategory =:fileCategory and f.status =:status order by f.sortNum,f.sort ";
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("contractId", contractId);
        map.put("fileCategory", fileCategory);
        map.put("status", status);

        return this.findHql(hql, map);
    }


    /**
     * 按家庭统计有效文件总数
     * fileType为档案类型
     *
     * @return
     */
    public Map<Integer, Map> getFileNum(String fileType) {
        StringBuffer sql = new StringBuffer("select contract_id,IFNULL(count(id),0) as allFileCount from file_info ");
        sql.append(" where status = 1 ");
        if (!fileType.equals("all")) {
            sql.append(" and file_type = '" + fileType + "' ");
        }
        sql.append(" group by contract_id");

        List<Map> mapList = this.findSql(sql.toString(), null);
        Map<Integer, Map> resultMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(mapList)) {
            for (Map map : mapList) {
                Integer contractId = Integer.valueOf(map.get("contract_id").toString());
                resultMap.put(contractId, map);
            }
        }
        return resultMap;
    }

    /**
     * 得到相应目录档案数量
     *
     * @param contractId
     * @param fileCategoryList
     */
    public Map<String, List<String>> getShNumber(Integer contractId, List<String> fileCategoryList) {
        Map<String, List<String>> resultMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(fileCategoryList)) {
            StringBuffer sql = new StringBuffer("select file_category,IFNULL(count(id),0) as allFile from file_info ");
            sql.append(" where contract_id =:contract_id and  status = 1 ");
            sql.append(" and file_category in  (:fileCategoryList)");
            sql.append(" group by file_category order by file_category");

            Map<String, Object> paramMap = Maps.newLinkedHashMap();
            paramMap.put("contract_id", contractId);
            paramMap.put("fileCategoryList", fileCategoryList);

            List<Map> resultList = this.findSql(sql.toString(), paramMap);


            List<String> resultStr = null;
            if (CollectionUtils.isNotEmpty(resultList)) {
                for (Map map : resultList) {
                    resultStr = Lists.newArrayList();
                    String fileCategory = map.get("file_category").toString();
                    String allFile = map.get("allFile") == null ? "0" : map.get("allFile").toString();
                    resultStr.add(0, allFile);
                    resultMap.put(fileCategory, resultStr);
                }
            }
        }
        return resultMap;
    }

    /**
     * 得到某一目录档案数量（多态）
     *
     * @param familyId
     * @param fileCategory
     */
    public int getShNumber(Integer contractId, String fileCategory) {
        StringBuffer sql = new StringBuffer(" from FileInfo f inner join fetch f.contract c  ");
        sql.append(" where c.id =:contractId and f.fileCategory =:fileCategory and f.status = 1");
        Map<String, Object> paramMap = Maps.newLinkedHashMap();
        paramMap.put("contractId", contractId);
        paramMap.put("fileCategory", fileCategory);

        List resultList = this.findHql(sql.toString(), paramMap);
        int result = 0;
        if (CollectionUtils.isNotEmpty(resultList)) {
            result = resultList.size();
        }
        return result;
    }


    /**
     * Map<fileCategory,List<FileInfo>>
     *
     * @param contractId
     * @return
     */
    public Map<String, List<FileInfo>> getFileInfoMap(Integer contractId) {
        String hql = " from FileInfo fi inner join fetch fi.contract c where fi.status = 1 and c.id = :contractId order by fi.sort";
        Map<String, Object> paramMap = Maps.newLinkedHashMap();
        paramMap.put("contractId", contractId);
        List<FileInfo> fileInfoList = this.findHql(hql.toString(), paramMap);
        Map<String, List<FileInfo>> resultMap = Maps.newLinkedHashMap();
        if (CollectionUtils.isNotEmpty(fileInfoList)) {
            List<FileInfo> itemList = Lists.newLinkedList();
            for (FileInfo fileInfo : fileInfoList) {
                String fileCategory = fileInfo.getFileCategory();
                itemList = resultMap.get(fileCategory) == null ? new LinkedList<FileInfo>() : resultMap.get(fileCategory);
                itemList.add(fileInfo);
                resultMap.put(fileCategory, itemList);
            }
        }
        return resultMap;
    }


    /**
     * 通过家庭id和档案类型删除文件
     *
     * @param contractId
     * @param fileCategory
     */
    public void deleteByFileCategory(Integer contractId, String fileCategory) {
        String hql = " update file_info fi set fi.status = 2,fi.del_date = NOW() where fi.contract_id = :contract_id and fi.file_category = :file_category";
        Map<String, Object> paramMap = Maps.newLinkedHashMap();
        paramMap.put("contract_id", contractId);
        paramMap.put("file_category", fileCategory);
        this.executeSql(hql, paramMap);
    }

    /**
     * 删除家庭下的文件
     *
     * @param familyId
     */
    public void deleteByFamilyId(Integer contractId) {
        String hql = " DELETE FROM file_info where contract_id = :contract_id";
        Map<String, Object> paramMap = Maps.newLinkedHashMap();
        paramMap.put("contract_id", contractId);
        this.executeSql(hql, paramMap);
    }

}
